<?php

namespace App\Admin\Controllers;

use App\Models\Administrator;
use App\Models\Category;
use App\Models\Product;

use App\Models\Proxy;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ProductsController extends Controller
{
    use ModelForm;

    private $header = '商品管理';
    private $description = '管理所有旗下商品';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form());
        });
    }

    public function show(Product $product)
    {
        return Admin::content(function (Content $content) use ($product) {
            $content->header('查看详情');
            $content->body(view('admin.products.show', ['product' => $product]));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Product::class, function (Grid $grid) {
            $grid->category()->name('分类');
            $grid->name('名称');
            $grid->description('描述');
            $grid->type('SKU');
            $grid->price('价格');
            $grid->actions(function ($actions) {
                $actions->prepend('<a href="' . route('admin.products.show', [$actions->getKey()]) . '"><i class="fa fa-eye"></i></a>');
            });
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Product::class, function (Form $form) {
            $categories = Category::pluck('name', 'id')->toArray();
            $form->select('category_id', '分类')->options($categories);
            $form->text('name', '名称')->rules('required');
            $form->text('description', '描述')->rules('required');
            $form->text('type', 'SKU')->rules('required');
            $form->image('image', '封面')->rules('required');
            $form->hasMany('skus', function (Form\NestedForm $form) {
                $form->decimal('price', '价格')->rules('required');
                $form->text('type', 'SKU')->rules('required');
            });
            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saving(function (Form $form) {
                $form->model()->price = collect($form->input('skus'))->where(Form::REMOVE_FLAG_NAME, 0)->min('price');
            });
        });
    }
}
